package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.mapper.ActiveMapper;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Collect;
import com.lingnan.mapper.CollectMapper;
import com.lingnan.service.CollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    ActiveMapper activeMapper;

    /**
     * 根据用户no查询收藏表（分页）
     * @param no
     * @return
     */
    @Override
    public List<Active> collectList(String no) {
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.select("active_id");
        collectQueryWrapper.eq("no", no);
        List<Map<String, Object>> maps = collectMapper.selectMaps(collectQueryWrapper);
        //将查询到的活动id添加到集合中
        Collection collection = new ArrayList();
        for (Map<String, Object> map : maps) {
            Integer id = (Integer)map.get("active_id");
            collection.add(id);
        }
        //判断收藏表里面是否有数据
        if (collection.size() != 0){
            System.out.println(collection);
            //对集合中的活动进行分页操作
            QueryWrapper<Active> activeQueryWrapper = new QueryWrapper<>();
            activeQueryWrapper.in("id", collection);
            List<Active> activeList = activeMapper.selectList(activeQueryWrapper);
            System.out.println(activeList);
            return activeList;
        }
        return null;
    }

    /**
     * 用户no和活动id删除收藏的活动（逻辑删除）
     * @param id
     * @param no
     * @return
     */
    @Override
    public boolean deleteCollect(Integer id, String no) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("active_id", id).eq("no", no);
        int delete = collectMapper.delete(wrapper);
        System.out.println(delete);
        return delete > 0;
    }

    /**
     * 根据用户no和活动id查询收藏表内容
     * @param id
     * @param no
     * @return
     */
    @Override
    public Collect selectByIdAndNo(Integer id, String no) {
        Collect collect = collectMapper.selectByIdAndNo(id, no);
        return collect;
    }

    @Override
    public boolean updateUseful(Integer id, String no) {
        return collectMapper.updateCollect(id, no);
    }
}
